package org.stvd.service.workflow.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.common.SecurityUserHolder;
import org.stvd.common.utils.IdWorker;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.DateUtil;
import org.stvd.entities.workflow.WfGroup;
import org.stvd.entities.workflow.WfUser;
import org.stvd.repository.workflow.WfGroupDao;
import org.stvd.repository.workflow.WfUserDao;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.workflow.WfGroupService;

@Service("WfGroupService")
@Transactional
public class WfGroupServiceImpl extends BaseServiceImpl<WfGroup> implements WfGroupService {

	@Resource
	private WfGroupDao wfGroupDao;
	@Resource
	private WfUserDao wfUserDao;

	@Override
	public QueryResult<WfGroup> wfGroupQuery(Integer start, Integer limit, String name, String code, String createUid) {
		return wfGroupDao.wfGroupQuery(start, limit, name, code, createUid);
	}

	@Override
	public List<WfGroup> listWfGroupByCondition(String code, String status) {
		return wfGroupDao.listWfGroupByCondition(code, status);
	}

	@Override
	public WfGroup getWfGroupByCode(String code) {
		return wfGroupDao.getWfGroupByCode(code);
	}

	@Override
	public ServiceResult<Map<String, Object>> insertWfGroup(WfGroup wfGroup) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		if (wfGroup == null) {
			serviceResult.setParamError();
			return serviceResult;
		}
		WfGroup originWfGroup = wfGroupDao.getWfGroupByCode(wfGroup.getCode());
		if (originWfGroup != null) {
			serviceResult.setDataError("分组编号已存在，请检查重新输入！");
			return serviceResult;
		}
		wfGroup.setId(IdWorker.getInstance().nextId());
		wfGroup.setCreateUid(SecurityUserHolder.getCurrentUserid());
		wfGroup.setCreateTime(DateUtil.getSystemDate());
		wfGroup.setModifyTime(DateUtil.getSystemDate());
		wfGroupDao.insert(wfGroup);
		serviceResult.setMessage("分组新增成功！");
		return serviceResult;
	}

	@Override
	public ServiceResult<Map<String, Object>> updateWfGroup(WfGroup wfGroup) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		if (wfGroup == null) {
			serviceResult.setParamError();
			return serviceResult;
		}
		WfGroup origionWfGroup=wfGroupDao.findByPk(WfGroup.class, wfGroup.getId());
		if(origionWfGroup==null) {
			serviceResult.setDataError("分组信息获取错误，请重试！");
			return serviceResult;
		}
		origionWfGroup.setName(wfGroup.getName());
		origionWfGroup.setStatus(wfGroup.getStatus());
		origionWfGroup.setContent(wfGroup.getContent());
		origionWfGroup.setModifyTime(DateUtil.getSystemDate());
		wfGroupDao.update(origionWfGroup);
		serviceResult.setMessage("分组修改成功！");
		return serviceResult;
	}

	@Override
	@Transactional
	public ServiceResult<Map<String, Object>> deleteWfGroup(Long groupId) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		if (groupId == null) {
			serviceResult.setParamError();
			return serviceResult;
		}
		WfGroup wfGroup = wfGroupDao.findByPk(WfGroup.class, groupId);
		if (wfGroup == null) {
			serviceResult.setDataError("分组信息获取失败！");
			return serviceResult;
		}
		List<WfUser> list = wfUserDao.listWfUserByCode(wfGroup.getCode());
		if (list != null && list.size() > 0) {
			serviceResult.setDataError("当前分组下绑定用户，禁止删除！");
			return serviceResult;
		}
		wfGroupDao.delete(WfGroup.class, groupId);
		serviceResult.setMessage("分组删除成功！");
		return serviceResult;
	}

	/**
	 * 更新分组用户
	 */
	@Override
	@Transactional
	public ServiceResult<Map<String, Object>> updateUserChooseInfo(String ids, String groupCode) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		if (ids == null) {
			return serviceResult;
		}
		if (ids.startsWith(",")) {
			ids = ids.substring(1);
		}
		List<WfUser> WfUserList = wfUserDao.listWfUserByCode(groupCode);
		if (WfUserList != null && WfUserList.size() > 0) {
			for (int i = 0; i < WfUserList.size(); i++) {
				WfUser user = WfUserList.get(i);
				user.setGroupCode(null);
				wfUserDao.update(user);
			}
		}
		WfGroup wfGroup = wfGroupDao.getWfGroupByCode(groupCode);
		wfGroup.setPerson(0);
		if (!ids.equals("") && ids.length() > 0) {
			String[] uids = ids.split(",");
			for (int i = 0; i < uids.length; i++) {
				WfUser wfUser = wfUserDao.findByPk(WfUser.class, Long.valueOf(uids[i]));
				wfUser.setGroupCode(groupCode);
				wfUser.setModifyTime(DateUtil.getSystemDate());
				wfUserDao.update(wfUser);
				wfGroup.setPerson(wfGroup.getPerson() + 1);
			}
		}
		wfGroupDao.update(wfGroup);
		return serviceResult;
	}

}
